package com.yx.frame.builder.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ServiceBuilderXml {

	String fileName = null;

	boolean isServiceBuilder= false;

	HashMap<String, MethodXml> methods = new HashMap<String, MethodXml>();

	public ServiceBuilderXml(String fileName) throws Exception {
		this.fileName = fileName;
		File file = new File(fileName);
		if (file.exists()) {
			analyzeXmlFile(file);
		} else {
			throw new Exception("文件：" + this.fileName + "不存在");
		}
	}

	public ServiceBuilderXml(File file) throws Exception {
		fileName = file.getPath() + file.getName();
		if (file.exists()) {
			analyzeXmlFile(file);
		} else {
			throw new Exception("文件：" + this.fileName + "不存在");
		}
	}

	private void analyzeXmlFile(File file) throws Exception {
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(file);
			Element root = document.getRootElement();
			if ("ServiceBuilder".equals(root.getName())) {
				isServiceBuilder = true;
				List<Element> methods = root.getChildren();
				for (int i = 0; i < methods.size(); i++) {
					Element method = methods.get(i);
					MethodXml methodXml = new MethodXml(method);
					this.addMethod(methodXml);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("解析文件失败:" + e);
		}
	}

	public String getFileName() {
		return fileName;
	}

	public boolean isServiceBuilder() {
		return isServiceBuilder;
	}

	public MethodXml getMethod(String name) {
		return methods.get(name);
	}
	public boolean containMehtodId(int mId){
		Iterator<MethodXml> methodIterator = methods.values().iterator();
		while (methodIterator.hasNext()) {
			MethodXml method = methodIterator.next();
			if(mId==method.getId()){
				return true;
			}
		}
		return false;
	}

	public boolean addMethod(MethodXml methodXml) {
		if (methods.containsKey(methodXml.getName())) {
			return false;
		}
		methods.put(methodXml.getName(), methodXml);
		return true;
	}

	public boolean replaceMethod(MethodXml methodXml) {
		if (methods.containsKey(methodXml.getName())) {
			methods.remove(methodXml.getName());
			methods.put(methodXml.getName(), methodXml);
			return true;
		} else {
			return false;
		}
	}

	public List<MethodXml> getMethods() {
		Iterator<MethodXml> methodIterator = methods.values().iterator();
		List<MethodXml> methodXmlList = new ArrayList<MethodXml>();
		while (methodIterator.hasNext()) {
			MethodXml method = methodIterator.next();
			methodXmlList.add(method);
		}
		methodXmlList = sort(methodXmlList);
		return methodXmlList;
	}

	public void removeMethod(String methodName) {
		if (methods.containsKey(methodName)) {
			methods.remove(methodName);
		}
	}

	public void save() throws Exception {
		File file = new File(fileName);
		if (file.exists()) {
			file.delete();
		}
		try {
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");			
			//FileWriter writer = new FileWriter(file);
			writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
			writer.write("<ServiceBuilder>\r\n");
			Iterator<MethodXml> methodIterator = methods.values().iterator();
			List<MethodXml> methodXmlList = new ArrayList<MethodXml>();
			while (methodIterator.hasNext()) {
				MethodXml method = methodIterator.next();
				methodXmlList.add(method);
			}
			methodXmlList = sort(methodXmlList);
			for (int i = 0; i < methodXmlList.size(); i++) {
				MethodXml method = methodXmlList.get(i);
				writer.write(method.toXml().toString());				
				writer.flush();
			}
			writer.write("</ServiceBuilder>\r\n");
			writer.flush();
			writer.close();
		} catch (Exception e) {
			throw new Exception("文件保存失败：" + e.getMessage());
		}

	}

	private List<MethodXml> sort(List<MethodXml> list) {
		MethodXml tmpParm;
		for (int i = 1; i < list.size(); i++) {
			for (int j = i; (j > 0)
					&& (list.get(j).getId() < list.get(j - 1).getId()); j--) {
				tmpParm = list.get(j);
				list.set(j, list.get(j - 1));
				list.set(j - 1, tmpParm);
			}
		}
		return list;
	}
}
